package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/model"
	"lot_admin_api/utility/response"
)

type sTenXunMap struct{}

var (
	insTenXunMap = sTenXunMap{}
)

func TenXunMap() *sTenXunMap {
	return &insTenXunMap
}

// TenXunMapBicycling 计算两地间的骑行距离
func (s *sTenXunMap) TenXunMapBicycling(ctx context.Context, req *api.TenXunMapBicyclingReq) (res *model.TenXunMapResultRes, err error) {
	r := g.RequestFromCtx(ctx)
	searchInfo := "/ws/direction/v1/walking?key=R3RBZ-ZAYKX-2YJ4Z-ZR75F-WXAAQ-BXF2D" + "&to=" + req.To + "&from=" + req.From
	resp, err := g.Client().Get(
		ctx,
		"https://apis.map.qq.com"+searchInfo,
	)
	if err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	var tenXunMapReturn *model.TenXunMapReturn
	defer resp.Close()
	redString := resp.ReadAllString()
	_ = gconv.Scan(redString, &tenXunMapReturn)
	if tenXunMapReturn.Status != 0 {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), tenXunMapReturn.Message)
	}
	_ = gconv.Scan(tenXunMapReturn.Result, &res)
	return res, err
}
